---
title: Use Scoring Code with Azure ML
description: Import Scoring Code models to Azure ML to make prediction requests using Azure.

---

# Use Scoring Code with Azure ML {: #use-scoring-code-with-azure-ml }

You must complete the following before importing Scoring Code models to Azure ML:

* Install the <a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest">Azure CLI client</a> to configure your service to the terminal.
* Install the <a target="_blank" href="https://docs.microsoft.com/en-us/azure/machine-learning/service/reference-azure-machine-learning-cli">Azure Machine Learning CLI extension</a>.

To import a Scoring Code model to Azure ML:

1. Login to Azure with the login command.

		az login

2. If you have not yet created a resource group, you can create one using <a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/group?view=azure-cli-latest#az-group-create">this command</a>:

		az group create --location --name [--subscription] [--tags]

	For example:

		az group create --location westus2 --name myresourcegroup


3. If you do not have an existing container registry that you want to use for storing custom Docker images, you must create one. If you want to use a DataRobot Docker image instead of building your own, you do not need to create a container registry. Instead, skip ahead to step 6.

	Create a container with <a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/acr?view=azure-cli-latest#az-acr-create">the following command</a>:

		az acr create --name --resource-group --sku {Basic | Classic | Premium | Standard}
		[--admin-enabled {false | true}] [--default-action {Allow | Deny}] [--location]
		[--subscription] [--tags] [--workspace]

	For example:

		az acr create --name mycontainerregistry --resource-group myresourcegroup --sku Basic

4. Set up admin access using <a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/acr?view=azure-cli-latest#az-acr-update">the following commands</a>:

		az acr update --name --admin-enabled {false | true}

	For example:

		az acr update --name mycontainerregistry --admin-enabled true

	And print the <a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/acr/credential?view=azure-cli-latest#az-acr-credential-show">registry credentials</a>:

		az acr credential show --name

	For example:

		az acr credential show --name mycontainerregistry

	Returns:

		{
		  "passwords": [
		    {
		      "name": "password",
		      "value": <password>
		    },
		    {
		      "name": "password2",
		      "value": <password>
		    }
		  ],
		  "username": mycontainerregistry
		}

5. Upload a custom Docker image that <a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/acr?view=azure-cli-latest#az-acr-build">runs Java</a>:

		az acr build --registry [--auth-mode {Default | None}] [--build-arg] [--file] [--image]
		[--no-format] [--no-logs] [--no-push] [--no-wait] [--platform] [--resource-group]
		[--secret-build-arg] [--subscription] [--target] [--timeout] []

	For example:

		az acr build --registry mycontainerregistry --image myImage:1 --resource-group myresourcegroup --file Dockerfile .

	The following is an example of a custom Docker image. Reference the <a target="_blank" href="https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-deploy-custom-docker-image#build-a-custom-base-image">Microsoft documentation</a> to read more about building an image.

		FROM ubuntu:16.04

		ARG CONDA_VERSION=4.5.12
		ARG PYTHON_VERSION=3.6

		ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
		ENV PATH /opt/miniconda/bin:$PATH

		RUN apt-get update --fix-missing && \
    		apt-get install -y wget bzip2 && \
    		apt-get clean && \
    		rm -rf /var/lib/apt/lists/*

		RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-${CONDA_VERSION}-Linux-x86_64.sh -O ~/miniconda.sh && \
    		/bin/bash ~/miniconda.sh -b -p /opt/miniconda && \
    		rm ~/miniconda.sh && \
    		/opt/miniconda/bin/conda clean -tipsy

		RUN conda install -y conda=${CONDA_VERSION} python=${PYTHON_VERSION} && \
    		conda clean -aqy && \
    		rm -rf /opt/miniconda/pkgs && \
    		find / -type d -name __pycache__ -prune -exec rm -rf {} \;

		RUN apt-get update && \
    		apt-get upgrade -y && \
    		apt-get install software-properties-common -y && \
    		add-apt-repository ppa:openjdk-r/ppa -y && \
    		apt-get update -q && \
    		apt-get install -y openjdk-11-jdk && \
    		apt-get clean

6. If you have not already created a workspace, <a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/ext/azure-cli-ml/ml/workspace?view=azure-cli-latest#ext-azure-cli-ml-az-ml-workspace-create">use the following command to create one</a>. Otherwise, skip to step 7.

		az ml workspace create --workspace-name [--application-insights] [--container-registry]
		[--exist-ok] [--friendly-name] [--keyvault] [--location] [--resource-group] [--sku]
		[--storage-account] [--yes]

	For example:

		az ml workspace create --workspace-name myworkspace --resource-group myresourcegroup

7. <a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/ext/azure-cli-ml/ml/model?view=azure-cli-latest#ext-azure-cli-ml-az-ml-model-register">Register your Scoring Code model</a> to the Azure model storage. 

    !!! note
        Make sure you have exported your Scoring Code JAR file from DataRobot before proceeding. You can download the JAR file from the [Leaderboard](sc-download-leaderboard) or from a [deployment](sc-download-deployment).

		az ml model register --name [--asset-path] [--cc] [--description] [--experiment-name]
		[--gb] [--gc] [--model-framework] [--model-framework-version] [--model-path]
		[--output-metadata-file] [--path] [--property] [--resource-group] [--run-id]
		[--run-metadata-file] [--sample-input-dataset-id] [--sample-output-dataset-id]
		[--tag] [--workspace-name] [-v]

	For example, to register model named `codegenmodel`:

		az ml model register --name codegenmodel --model-path 5cd071deef881f011a334c2f.jar --resource-group myresourcegroup --workspace-name myworkspace

8. Prepare two configs and a <a target="_blank" href="https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-deploy-existing-model#entry-script">Python entry script</a> that will execute the prediction.

	Below are some examples of configs with a Python entry script.

	*  `deploymentconfig.json`:

			{
		    	"computeType": "aci",
		    	"containerResourceRequirements": {
					"cpu": 0.5,
					"memoryInGB": 1.0
		    	},
		    	"authEnabled": true,
		    	"sslEnabled": false,
		    	"appInsightsEnabled": false
			}

	* `inferenceconfig.json` (if you are <em>not</em> using a DataRobot Docker image):

			{
    			"entryScript": "score.py",
    			"runtime": "python",
    			"enableGpu": false,
    			"baseImage": "<container-registry-name>.azurecr.io/<Docker-image-name>",
    			"baseImageRegistry": {
        			"address": "<container-registry-name>.azurecr.io",
        			"password": <password from the step 2>,
        			"username": <container-registry-name>
      			}
 			}

	* `inferenceconfig.json`(if you <em>are</em> using a DataRobot Docker image):


			{
    			"entryScript": "score.py",
    			"runtime": "python",
    			"enableGpu": false,
    			"baseImage": "datarobotdev/scoring-inference-code-azure:latest",
				"baseImageRegistry": {
					"address": "registry.hub.docker.com"
				}
			}


	* `score.py`:

			import os
			import subprocess
			import tempfile
			import json
			from azureml.core import Model

			# Called when the deployed service starts

			def init():
    		pass

			# Handle requests to the service

			def run(data):
    			try:
        			result_csv = ''
        			data = json.loads(data)
        			# Access your model registered in step 6
        			model_path = Model.get_model_path('codegenmodel')
        			with tempfile.NamedTemporaryFile() as output_file:
            			p = subprocess.run(['java', '-jar', model_path, 'csv', '--input=-',
        	'--output={}'.format(output_file.name)], input=bytearray(data['csv'].encode('utf-8')), stdout=subprocess.PIPE)
                		with open(output_file.name) as result_file:
							result_csv = result_file.read()

			# Return prediction

			return result_csv
			except Exception as e:
				error = str(e)
				return error

9. <a target="_blank" href="https://docs.microsoft.com/en-us/cli/azure/ext/azure-cli-ml/ml/model?view=azure-cli-latest#ext-azure-cli-ml-az-ml-model-deploy">Create a new prediction endpoint</a>:

		az ml model deploy --name [--ae] [--ai] [--ar] [--as] [--at] [--autoscale-max-replicas]
		[--autoscale-min-replicas] [--base-image] [--base-image-registry] [--cc] [--cf]
		[--collect-model-data] [--compute-target] [--compute-type] [--cuda-version] [--dc]
		[--description] [--dn] [--ds] [--ed] [--eg] [--entry-script] [--environment-name]
		[--environment-version] [--failure-threshold] [--gb] [--gc] [--ic] [--id] [--kp]
		[--ks] [--lo] [--max-request-wait-time] [--model] [--model-metadata-file] [--namespace]
		[--no-wait] [--nr] [--overwrite] [--path] [--period-seconds] [--pi] [--po] [--property]
		[--replica-max-concurrent-requests] [--resource-group] [--rt] [--sc] [--scoring-timeout-ms]
		[--sd] [--se] [--sk] [--sp] [--st] [--tag] [--timeout-seconds] [--token-auth-enabled]
		[--workspace-name] [-v]

	For example, to create a new endpoint with the name `myservice`:

		az ml model deploy --name myservice --model codegenmodel:1 --compute-target akscomputetarget --ic inferenceconfig.json --dc deploymentconfig.json --resource-group myresourcegroup --workspace-name myworkspace

10. <a target="_blank" href="https://docs.microsoft.com/en-us/azure/machine-learning/how-to-setup-authentication">Get a token</a> to make prediction requests:

		az ml service get-keys --name [--path] [--resource-group] [--workspace-name] [-v]

	For example:

		az ml service get-keys --name myservice --resource-group myresourcegroup --workspace-name myworkspace

	This command returns a JSON response:

		{
		    "primaryKey": <key>,
		    "secondaryKey": <key>
		}

You can now make prediction requests using Azure.
